package web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import tool.DateUtils;
import vo.Opinion;
import vo.Paper;
import vo.PaperType;
import vo.Sentence;
import vo.SentenceType;
import business.OpinionService;
import business.PaperService;
import business.PaperTypeService;
import business.SentenceService;
import business.SentenceTypeService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class PaperAction implements Preparable {
	private PaperService service;
	private SentenceService sentenceService;
	private OpinionService opinionService;
	private PaperTypeService paperTypeService;
	private SentenceTypeService sentenceTypeService;
	private List<Paper> papers;
	private List<Opinion> opinions;
	private List<SentenceType> sentenceTypes;
	private List<Sentence> sentences;
	private List<PaperType> paperTypes;

	// 添加paper
	private String title;
	private String author;
	private String source;
	private String year;
	private String affiliation;
	private Integer vol;
	private Integer no;
	private String url;
	private int totalCount;
	private int type;

	// 分页的变量
	private int curPage;
	private int totalPage;
	private int pageSize = 6;

	// search
	private String keyword;

	private long paperCount;
	private long sentenceCount;
	private long opinionCount;

	public String listPaper() {
		paperCount = service.getTotalCount();
		sentenceCount = sentenceService.getTotalCount();
		opinionCount = opinionService.getTotalCount();
		if (keyword == null)
			keyword = "";
		if (curPage == 0)
			curPage = 1;

		// 计算总页面数
		totalCount = (int) service.getTotalCountByTitle("%" + keyword + "%");
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);

		// 检索本页的论文列表
		this.papers = service.getAllByTitle((curPage - 1) * pageSize, pageSize, "%" + keyword + "%");
		System.out.println("paper count:" + this.papers.size());

		// 为了下来列表使用
		this.paperTypes = paperTypeService.getAll();

		System.out.println("list paper:" + keyword);
		System.out.println("curPage:" + curPage);
		System.out.println("totalCount:" + totalCount);

		return Action.SUCCESS;
	}

	// 添加paper
	public String insertPaper() {
		System.out.println(title + "," + author + "," + source + "," + year + "," + affiliation + "," + vol + "," + no + "," + url + "," + type);
		Paper paper = new Paper();
		paper.setTitle(title);
		paper.setAuthor(author);
		paper.setSource(source);
		paper.setYear(year);
		paper.setAffiliation(affiliation);
		paper.setVol(vol);
		paper.setNo(no);
		paper.setUrl(url);
		Date today = new Date();
		String createdAt = DateUtils.date2Str(today, "-");
		paper.setCreatedAt(createdAt);
		PaperType tmp = paperTypeService.getById((long) type);
		paper.setPaperType(tmp);

		service.insert(paper);
		return "insertPaper";
	}

	// search paper
	public String searchPaper() {
		System.out.println("search:" + keyword);
		return "searchPaper";
	}

	public String updateCounts() {
		this.papers = service.getAll();
		for (Paper paper : papers) {
			int sentenceCount = (int) sentenceService.getTotalCountByPaperId(paper.getId());
			int opinionCount = (int) opinionService.getTotalCountByPaperId(paper.getId());
			System.out.println("sentenceCount:" + sentenceCount);
			System.out.println("opinionCount:" + opinionCount);
			paper.setSentenceCount(sentenceCount);
			paper.setOpinionCount(opinionCount);
			service.update(paper);
		}
		return Action.SUCCESS;

	}

	public String index() {
		System.out.println("index");
		return Action.SUCCESS;
	}

	public List<Paper> getPapers() {
		return papers;
	}

	public void prepare() throws Exception {
		// if (id != null)
		// employee = service.find(id);
	}

	public void setPaperService(PaperService service) {
		this.service = service;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public void setService(PaperService service) {
		this.service = service;
	}

	public void setPapers(List<Paper> papers) {
		this.papers = papers;
	}

	public String getAffiliation() {
		return affiliation;
	}

	public void setAffiliation(String affiliation) {
		this.affiliation = affiliation;
	}

	public List<Sentence> getSentences() {
		return sentences;
	}

	public void setSentences(List<Sentence> sentences) {
		this.sentences = sentences;
	}

	public void setSentenceService(SentenceService sentenceService) {
		this.sentenceService = sentenceService;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public List<PaperType> getPaperTypes() {
		return paperTypes;
	}

	public void setPaperTypes(List<PaperType> paperTypes) {
		this.paperTypes = paperTypes;
	}

	public void setPaperTypeService(PaperTypeService paperTypeService) {
		this.paperTypeService = paperTypeService;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public List<SentenceType> getSentenceTypes() {
		return sentenceTypes;
	}

	public void setSentenceTypes(List<SentenceType> sentenceTypes) {
		this.sentenceTypes = sentenceTypes;
	}

	public void setSentenceTypeService(SentenceTypeService sentenceTypeService) {
		this.sentenceTypeService = sentenceTypeService;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List<Opinion> getOpinions() {
		return opinions;
	}

	public void setOpinions(List<Opinion> opinions) {
		this.opinions = opinions;
	}

	public void setOpinionService(OpinionService opinionService) {
		this.opinionService = opinionService;
	}

	public Integer getVol() {
		return vol;
	}

	public void setVol(Integer vol) {
		this.vol = vol;
	}

	public Integer getNo() {
		return no;
	}

	public void setNo(Integer no) {
		this.no = no;
	}

	public long getPaperCount() {
		return paperCount;
	}

	public void setPaperCount(long paperCount) {
		this.paperCount = paperCount;
	}

	public long getSentenceCount() {
		return sentenceCount;
	}

	public void setSentenceCount(long sentenceCount) {
		this.sentenceCount = sentenceCount;
	}

	public long getOpinionCount() {
		return opinionCount;
	}

	public void setOpinionCount(long opinionCount) {
		this.opinionCount = opinionCount;
	}

}